<script setup lang="ts">

import {DocumentTextSharp, HandLeft, Notifications, Open} from "@vicons/ionicons5"
import {nextTick, onMounted, ref, watchEffect} from "vue";
import {
  CreateTask,
  DeleteTask,
  GetAppConfig,
  GetLogContent,
  GetTaskStateOK,
  OpenWindowsTasker,
  SetAppConfig
} from "../../wailsjs/go/main/App";
import {LogInst, useMessage} from "naive-ui";

const message = useMessage();
const taskOpened = ref(false);
const notificationEnabled = ref(false);

const logInstRef = ref<LogInst | null>(null)
const showLogModal = ref(false);
const logContent = ref('')

function onViewLog() {
  showLogModal.value = true
  GetLogContent().then(content => {
    logContent.value = content
    // logInstRef.value.scrollTo({position: "bottom", silent: true})
  })
}

GetAppConfig().then(config => {
  notificationEnabled.value = config.notification_enabled
})

function onOpenTasker() {
  OpenWindowsTasker()
}

function onNotificationChange(enabled) {
  GetAppConfig().then(async config => {
    config.notification_enabled = enabled
    await SetAppConfig(config)
  })
}


function onTaskStateChange(enabled) {
  console.log("onTaskStateChange", enabled)
  if (enabled) {
    CreateTask().then(() => {
      updateTaskStateFromWindows()
    }).catch(err => {
      taskOpened.value = false
      message.error(err)
    })
  } else {
    DeleteTask().then(() => {
      updateTaskStateFromWindows()
    })
  }
}

function updateTaskStateFromWindows() {
  GetTaskStateOK().then(ok => {
    console.log("updateTaskStateFromWindows", ok)
    taskOpened.value = ok
  })
}

onMounted(() => {
  watchEffect(() => {
    if (logContent.value) {
      nextTick(() => {
        logInstRef.value?.scrollTo({position: 'bottom', silent: true})
      })
    }
  })
})

updateTaskStateFromWindows()

</script>

<template>


  <n-modal v-model:show="showLogModal" preset="dialog">
    <template #header>
      <n-space>
        <span>日志</span>
      </n-space>
    </template>
    <n-log :log="logContent" ref="logInstRef" trim>

    </n-log>
  </n-modal>

  <n-card>
    <n-flex vertical align="center" justify="center">
      <n-tooltip trigger="hover">
        <template #trigger>
          <n-flex>
            <n-icon>
              <HandLeft/>
            </n-icon>
            <span>自动认证</span>
            <n-switch v-model:value="taskOpened" :on-update-value="onTaskStateChange"></n-switch>
          </n-flex>
        </template>
        在 <b>进入桌面</b> 和 <b>切换Wi-Fi</b> 时自动检测并进行校园网认证
        <br/>
        此功能依赖于 Windows计划任务程序
        <br/>

        <b>切换Wi-Fi: </b>
        <i>CFNMXX, YYJSWIFI-5G, YYJSWIFI-2.4G</i>
      </n-tooltip>

      <n-tooltip trigger="hover">
        <template #trigger>
          <n-space>
            <n-icon>
              <Notifications/>
            </n-icon>
            <span>发送通知</span>
            <n-switch v-model:value="notificationEnabled" @change="onNotificationChange"></n-switch>
          </n-space>
        </template>
        <span>通过系统通知告知认证结果</span>
      </n-tooltip>

      <br/>
      <n-tooltip trigger="hover">
        <template #trigger>
          <n-button @click="onOpenTasker">
            打开Windows计划任务程序

            <template #icon>
              <Open/>
            </template>
          </n-button>
        </template>
        打开后可前往 <b>任务计划程序库 -> 校园网工具</b> 查看计划任务
      </n-tooltip>

      <n-button @click="onViewLog">查看日志
        <template #icon>
          <DocumentTextSharp/>
        </template>
      </n-button>
    </n-flex>
  </n-card>
</template>

<style scoped>
.has-number {
  margin-bottom: 8px;
}
</style>